package com.vincent.controller.admin;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.common.enums.SessionEnum;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.service.PermissionService;
import com.vincent.vo.PermissionAddVo;
import com.vincent.vo.PermissionEditVo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by vincent on 15-1-3.
 */
@Controller
@RequestMapping("/admin/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    /**
     * 跳转到权限管理页面
     * @return
     */
    @RequestMapping("/manage")
    public String manage(){
        return "/admin/permission/manage";
    }

    /**
     * 获取权限信息
     * @return
     */
    @RequestMapping(value = "/getManagePermissionsAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody Object getManagePermissionsAjax(){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Map<String, Object> serviceMap = permissionService.getPermissions();
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),errorObj);
            return resultMap;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return resultMap;
    }
    @RequestMapping("/add")
    public String add(@RequestParam(required = false) Integer parentId,HttpServletRequest request){
        request.setAttribute("parentId",parentId);
        return "/admin/permission/add";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Integer id,HttpServletRequest request,HttpSession session){
        Map<String, Object> serviceMap = permissionService.getPermission(id);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/permission/manage.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        request.setAttribute("permission",successObj);
        return "/admin/permission/edit";
    }
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute PermissionAddVo vo,Errors errors,HttpSession session){
        Map<String, Object> serviceMap = permissionService.savePermission(vo);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/permission/add.html"+(vo.getParentId()==null?"":"?parentId="+vo.getParentId());
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/permission/manage.html";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute PermissionEditVo vo,Errors errors,HttpSession session){
        Map<String, Object> serviceMap = permissionService.updatePermission(vo);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/permission/edit/"+vo.getId()+".html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/permission/manage.html";
    }

    /**
     * 删除权限
     * @return
     */
    @RequestMapping(value = "/deletePermissionsAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody Object deletePermissionsAjax(@RequestParam("ids[]")Integer []ids){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Map<String, Object> serviceMap = permissionService.deletePermission(ids);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),errorObj);
            return resultMap;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return resultMap;
    }
}
